class Solution {
    vector<int> tmp;
public:
    vector<int> sortArray(vector<int>& nums) {
        tmp.resize(nums.size());
        mergeSort(nums, 0, nums.size() - 1);
        return nums;
    }
 
    void mergeSort(vector<int>& nums, int left, int right)
    {
        if(left >= right)
            return;
        // 选择中间点划分两个区间并排序 [left, mid] [mid + 1, right]
        int mid = (left + right) >> 1;
        mergeSort(nums, left, mid);
        mergeSort(nums, mid + 1, right);
        // 合并两个有序数组，i是全局数组tmp下标
        int cur1 = left, cur2 = mid + 1, i = 0;
        while(cur1 <= mid && cur2 <= right)
            tmp[i++] = nums[cur1] < nums[cur2] ? nums[cur1++] : nums[cur2++];
        // 处理没遍历完的数组，只有一个while可进
        while(cur1 <= mid)
            tmp[i++] = nums[cur1++];
        while(cur2 <= right)
            tmp[i++] = nums[cur2++];
        // 还原数组
        for(int j = left; j <= right; ++j)
            nums[j] = tmp[j - left];
    }
};